package com.eqdd.yiqidian.ui.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ProjectExpListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.SwipeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.suke.widget.SwitchButton;

import java.util.ArrayList;
import java.util.List;

public class ProjectExpShowActivity extends BaseActivity {
    private RecyclerViewRefreshCustom dataBinding;
    private SlimAdapterEx<ProjectExpListResultBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "项目经验");
        initTopRightText("添加", (v) -> {
            startActivityForResult(AddProjectExpActivity.class, null, Config.ADD_PROJECT_EXP);
        });
    }

    @Override
    public void initData() {
        SwipeUtil.openSwipe(this, dataBinding.recyclerView, (menuBridge, pos) -> {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_PROJECT_EXP)
                    .params("userGuid", user.getGuid())
                    .params("projectepid", slimAdapterEx.getDataItem(pos).getId())
                    .execute(new DialogCallBack<HttpResult>(ProjectExpShowActivity.this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            menuBridge.closeMenu();
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                slimAdapterEx.getData().remove(pos);
                                slimAdapterEx.notifyItemRemoved(pos);
                            }
                        }
                    });
        });
        dataBinding.recyclerView.setSwipeItemClickListener((view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("bean", slimAdapterEx.getDataItem(position));
            startActivityForResult(ProjectExpInfoActivity.class, bundle, Config.PROJECT_EXP_INFO);
        });
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_61, new SlimInjector<ProjectExpListResultBean>() {
            @Override
            public void onInject(ProjectExpListResultBean data, IViewInjector injector) {
                injector.text(R.id.tv_upper_left_content, data.getOwnedCompany())
                        .text(R.id.tv_down_left_content, data.getStartTime() + "~" + data.getEndTime())
                        .with(R.id.sb_right, view -> {
                            SwitchButton switchButton = (SwitchButton) view;
                            switchButton.setChecked(data.isIsOvert());
                            switchButton.setOnCheckedChangeListener((view1, isChecked) -> {
                                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.TOGGLE_PROJECT_EXP_OVERT)
                                        .params("userGuid", user.getGuid())
                                        .params("projectepid", data.getId())
                                        .params("isOvert", isChecked ? 1 : 0)
                                        .execute(new DialogCallBack<HttpResult>(ProjectExpShowActivity.this) {
                                            @Override
                                            public void onSuccess(Response<HttpResult> response) {
                                                HttpResult httpResult = response.body();
                                                ToastUtil.showShort(httpResult.getMsg());
                                                if (httpResult.getStatus() == 200) {
                                                    data.setIsOvert(isChecked);
                                                }
                                            }

                                            @Override
                                            public void onError(Response<HttpResult> response) {
                                                super.onError(response);
                                                slimAdapterEx.notifyItemChanged(slimAdapterEx.getData().indexOf(data));
                                            }
                                        });
                            });
                        });
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());


        RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> refresh());
        dataBinding.coolRefreshView.setRefreshing(true);
    }

    private void refresh() {
        OkGo.<HttpResult<List<ProjectExpListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.PROJECT_EXP_LIST)
                .params("userGuid", user.getGuid())
                .params("peuserGuid", user.getGuid())
                .execute(new DialogCallBack<HttpResult<List<ProjectExpListResultBean>>>(ProjectExpShowActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<ProjectExpListResultBean>>> response) {
                        HttpResult<List<ProjectExpListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        dataBinding.coolRefreshView.setRefreshing(false);
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }

                    @Override
                    public void onError(Response<HttpResult<List<ProjectExpListResultBean>>> response) {
                        super.onError(response);
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }
                });
    }


    @Override
    public void setView() {


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS && requestCode == Config.ADD_PROJECT_EXP) {
            refresh();
        }
        if (resultCode == Config.CHANGE && requestCode == Config.PROJECT_EXP_INFO) {
            refresh();
        }
    }

}
